package com.rongke.doctor.web.medical;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rongke.biz.service.MedicalRecordManager;
import com.rongke.commons.JsonResp;
import com.rongke.enums.MedicalRecordStatusEnum;
import com.rongke.model.MedicalRecords;
import com.rongke.service.MedicalRecordsService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 医生端病历相关controller
 * 
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value = "/api/doctor/medicalRecord")
@Transactional
public class DoctorMedicalController {
	private static final Logger LOGGER = Logger
			.getLogger(DoctorMedicalController.class);

	@Autowired
	private MedicalRecordsService medicalRecordsService;

	@Autowired
	private MedicalRecordManager medicalRecordManager;

	/**
	 * 查询状态=1、2的所有病历基本信息，即待审核状态的病历 <br>
	 * TODO:增加考虑：病历状态=1/2，但是治疗建议还没有生成的情况。因为生成建议是异步过程
	 * 
	 */
	@RequestMapping(value = "/queryNoVerified", method = RequestMethod.GET)
	public JsonResp queryMedicalRecord(Page<MedicalRecords> page) {
		LOGGER.debug("queryMedicalRecord by doctor");

		try {
			EntityWrapper<MedicalRecords> ew = new EntityWrapper<>();
			ew.in("status",
					new Integer[] {
							MedicalRecordStatusEnum.AUTHORIZE.getStatus(),
							MedicalRecordStatusEnum.MODIFIED.getStatus() });
			ew.orderBy("gmt_modified", false);
			Page<MedicalRecords> medicalRecordsPage = medicalRecordsService
					.selectPage(page, ew);
			return JsonResp.ok(medicalRecordsPage);
		} catch (Exception e) {
			LOGGER.error("queryMedicalRecord by doctor failed", e);
			return JsonResp.fa(null);
		}
	}

	/**
	 * 根据病历ID查询病历基本信息
	 * 
	 * @param medicalRecordId
	 * @return
	 */
	@RequestMapping(value = "/queryBasic", method = RequestMethod.GET)
	public JsonResp querySingleMedicalRecord(String medicalRecordId) {
		LOGGER.debug("querySingleMedicalRecord by doctor");

		try {
			MedicalRecords medicalRecord = medicalRecordsService
					.selectById(medicalRecordId);
			return JsonResp.ok(medicalRecord);
		} catch (Exception e) {
			LOGGER.error("queryMedicalRecord by doctor failed", e);
			return JsonResp.fa(null);
		}
	}

	/**
	 * 根据病历ID查询时间轴
	 * 
	 * @param medicalRecordId
	 * @return
	 */
	@RequestMapping(value = "/queryTimeline", method = RequestMethod.GET)
	public JsonResp queryTimeline(long medicalRecordId) {
		LOGGER.debug("queryTimeline by doctor");

		try {
			List<JSONObject> totalRecordList = medicalRecordManager
					.getDetailTimeline(medicalRecordId);

			return JsonResp.ok(totalRecordList);

		} catch (Exception e) {
			LOGGER.error("getTimeLine failed", e);
			return JsonResp.fa(null);
		}
	}
}
